package com.benmei.ntlive.web;


import com.benmei.ntlive.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * Created by Peter on 2017/8/18.
 */
@RestController
public class LiveCallbackController {
    private static final Logger logger = LoggerFactory.getLogger(LiveCallbackController.class);

    /**
     * http://120.26.224.203:9201/LiveMixNotify
     * 连麦回调。当用户连麦成功后阿里直播服务器会回调该地址，通知我们连麦成功。
     *
     * @return
     */
    @RequestMapping("/LiveMixNotify")
    public String liveMixNotify(@RequestBody(required = false) String body, HttpServletRequest request, HttpServletResponse response) {
        // 打印请求参数
        StringBuffer sb = getRequestParamsString(request);
        sb.append("--- body ---\n").append(body).append("\n\n");
        logger.info(sb.toString());
        return "ok";
    }

    /**
     * 推流回调,流状态实时信息回调，可以及时通知用户推流或断流操作结果。
     * <h3>注意事项</h3>
     * <ul>
     * <li>原理：通过 HTTP 接口向用户服务器发送GET请求，将视频流推送成功，断流成功的状态实时反馈给用户，用户服务器通过 200 响应返回接口返回结果。</li>
     * <li>URL无需标识，只需可正常访问，URL 的应答有要求如下：</li>
     * <li>如果访问超时，会重试这个 URL，目前超时时间是 5s，重试次数是 5 次，重试间隔为 1s。</li>
     * </ul>
     * <h3>举例说明</h3>
     *<p>e.g. http://1.1.1.1?action=publish&app=xc.cdnpe.com&appname=hello&id=world&ip=42.120.74.183&node=cdnvideocenter010207116011.cm3</p>
     * <table>
     * <tr><th>参数</th><th>取值说明</th></tr>
     * <tr><td>time</td><td>unix时间戳</td></tr>
     * <tr><td>usrargs</td><td>用户推流的参数</td></tr>
     * <tr><td>action</td><td>publish表示推流，publish_done表示断流</td></tr>
     * <tr><td>app</td><td>app	默认为自定义的推流域名，如果未绑定推流域名即为播放域名</td></tr>
     * <tr><td>appname</td><td>应用名称</td></tr>
     * <tr><td>id</td><td>流名称</td></tr>
     * <tr><td>node</td><td>cdn接受流的节点或者机器名</td></tr>
     * <tr><td>ip</td><td>推流的客户端ip</td></tr>
     * </table>
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/PushStreamNotify")
    public String pushStreamNotify(HttpServletRequest request, HttpServletResponse response) {
        // 打印请求参数
        StringBuffer sb = getRequestParamsString(request);
        logger.info(sb.toString());
        return "ok";
    }

    /**
     * 录制回调
     *
     * @param body
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/RecordStreamNotify")
    public String recordStreamNotify(@RequestBody(required = false) String body, HttpServletRequest request, HttpServletResponse response) {
        // 打印请求参数
        StringBuffer sb = getRequestParamsString(request);
        sb.append("--- body ---\n").append(body).append("\n\n");
        logger.info(sb.toString());
        return "ok";
    }

    private StringBuffer getRequestParamsString(HttpServletRequest request) {
        String clientIp = HttpUtil.getIp(request);
        StringBuffer sb = new StringBuffer("\n ------------------------------------------------------------ request from :");
        sb.append(clientIp).append("        ").append(request.getRequestURI()).append("--------------------------------------------------------------->>>>>>>>> ");

        sb.append("\n --- headers --- \n");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            sb.append(header).append(":").append(headerNames.nextElement());
        }

        String qs = request.getQueryString();
        sb.append("\n --- QueryString -- \n").append(qs);
        sb.append("\n --- params ---------------------------------\n");
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = request.getParameter(name);
            sb.append(name).append(":").append(value).append("\n");
        }
        return sb;
    }
}
